/*
 * URLReader.java
 * This file is part of the <a href="http://code.google.com/p/pux">PUX Framework</a>
 *
 * Copyright 2008 by <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package de.teamskill.pux.library.io;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import de.teamskill.pux.engine.AbstractNode;
import de.teamskill.pux.engine.DataElement;
import de.teamskill.pux.engine.InputPort;
import de.teamskill.pux.engine.OutputPort;

/**
 * URLReader reads a resource as given by a URL and provides its content as an
 * InputStream
 * 
 * @author <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 * 
 */
public class URLReader extends AbstractNode {

	/**
	 * CONFIG_URL
	 */
	public static final String CONFIG_URL = "url";

	/**
	 * OUT_STREAM
	 */
	public static final String OUT_STREAM = "OUT";

	/**
	 * url
	 */
	private InputPort url;

	/**
	 * inputStream
	 */
	private OutputPort inputStream;

	/**
	 * URLReader Constructor
	 * 
	 */
	public URLReader() {
		this("URLReader" + nr++, false);
	}

	/**
	 * URLReader Constructor
	 * 
	 * @param nodeName
	 * @param continuous
	 */
	public URLReader(String nodeName, boolean continuous) {
		super(nodeName, continuous);
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#compute()
	 */
	public void compute() {
		try {
			URL u = new URL((String) url.fetchData());
			try {
				InputStream is = u.openStream();
				DataElement de = new DataElement(is);
				de.annotate(CONFIG_URL, u.toExternalForm());
				inputStream.pushBuffer(de);
			} catch (IOException e) {
				throw new IllegalStateException("Failed to open " + u.toExternalForm() + ": "
						+ e.getLocalizedMessage());
			}
		} catch (MalformedURLException e) {
			throw new IllegalArgumentException("Invalid URL", e);
		}
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#createPorts()
	 */
	protected void createPorts() {
		url = addConfigurationPort(CONFIG_URL, String.class, true);
		inputStream = addOutputPort(OUT_STREAM, InputStream.class);
	}

	/**
	 * JavaBean-style getter-method for url
	 * 
	 * @return the url
	 */
	public InputPort getUrl() {
		return url;
	}

	/**
	 * /** JavaBean-style setter-method for url
	 * 
	 * @param url
	 *            the url to set
	 */
	public void setUrl(InputPort url) {
		this.url = url;
	}

	/**
	 * JavaBean-style getter-method for inputStream
	 * 
	 * @return the inputStream
	 */
	public OutputPort getInputStream() {
		return inputStream;
	}

	/**
	 * /** JavaBean-style setter-method for inputStream
	 * 
	 * @param inputStream
	 *            the inputStream to set
	 */
	public void setInputStream(OutputPort inputStream) {
		this.inputStream = inputStream;
	}

}
